<?php 

include ('configure.php');
include("class.db.php");


$msql  = new Db;
$msql->connect();

//system("echo \"server server\" >".SNXRC_WORK);

$admin_yes=null;
$user_yes=null;
$Ftp_1_yes=null;
$Ftp_2_yes=null;
$stato=null;
$All_yes=null;

if( ($_POST['oldPasswordAdmin'] != undefined) || ($_POST['newPasswordAdmin'] != undefined)) 
 {
    
    $passwordAdmin=md5($_POST['oldPasswordAdmin']);
    $newpasswordAdmin=md5($_POST['newPasswordAdmin']);
    $admin_yes=0;
    //now validating the user and the password
    $sql="SELECT user_name, password FROM login WHERE user_name='admin'";
    $result=$msql->execute($sql);
    $row=mysql_fetch_array($result);

//    $sql="SELECT user_name, password FROM login WHERE user_name='admin'";
//    $result=mysql_query($sql);
//    $row=mysql_fetch_array($result);

    //if username exists
    if(mysql_num_rows($result)>0)
    {
        //compare the password
        if(strcmp($row['password'],$passwordAdmin)==0)
        {
            $query = "UPDATE login SET password='".$newpasswordAdmin."' WHERE user_name='admin'";
            $admin_yes=($msql->execute($query)) ? 1: 0;
        }  
    }
 }
 else if( ($_POST['oldPasswordUser'] != undefined) || ($_POST['newPasswordUser'] != undefined) )
 {
     
    $passwordUser=md5($_POST['oldPasswordUser']);
    $newpasswordUser=md5($_POST['newPasswordUser']);
    
    $user_yes=0;
    //now validating the user and the password
    $sql="SELECT user_name, password FROM login WHERE user_name='user'";
    $result=$msql->execute($sql);
    $row=mysql_fetch_array($result);

    //if username exists
    if(mysql_num_rows($result)>0)
    {
        //compare the password
        if(strcmp($row['password'],$passwordUser)==0)
        {

            $query = "UPDATE login SET password='".$newpasswordUser."' WHERE user_name='user'";
            $user_yes=($msql->execute($query)) ? 1: 0;
        }  
    }
 }
 else if( ($_POST['ftpIP_1'] != undefined)       || 
          ($_POST['ftpUser_1'] != undefined)     || 
          ($_POST['ftpPassword_1'] != undefined) || 
          ($_POST['ftpDate_1'] != undefined)     ||
          ($_POST['vpnServer'] != undefined)     ||
          ($_POST['vpnUser'] != undefined)     ||
          ($_POST['vpnPassword'] != undefined)     
         )
 {
    $IPFtp_1=($_POST['ftpIP_1']);
    $UserFtp_1=($_POST['ftpUser_1']);
    $PasswordFtp_1=($_POST['ftpPassword_1']);
    $ftpName_1=$_POST['ftpName_1'];
    $ftpGiorni_1=$_POST['ftpDate_1'];
    
    $vpnServer=$_POST['vpnServer'];
    $vpnUser=$_POST['vpnUser'];
    $vpnPassword=$_POST['vpnPassword'];
    
    $data_sec=time() + (60* 60* 24 * $ftpGiorni_1);
    $data_scedulazione=strftime('%Y-%m-%d %H:%M:%S',$data_sec);
    $query = "UPDATE ftp_table SET ftp_password='".$PasswordFtp_1."',ftp_user='".$UserFtp_1."',ftp_ip='".$IPFtp_1."',
            ftp_date=".$ftpGiorni_1.",vpn_server='".$vpnServer."',vpn_user='".$vpnUser."',
            vpn_password='".$vpnPassword."', data_scadenza='".$data_scedulazione."' WHERE ftp_name='".$ftpName_1."'";
    $Ftp_1_yes=($msql->execute($query)) ? 1: 0;
    if($Ftp_1_yes)
    {
   
        system("echo \"server $vpnServer\" >".SNXRC_WORK);
        system("echo \"username $vpnUser\" >>".SNXRC_WORK);
        system("echo \"#password $vpnPassword\" >>".SNXRC_WORK);
   }
 }
 else if( ($_POST['ftpIP_2'] != undefined)      || 
          ($_POST['ftpUser_2'] != undefined)    || 
          ($_POST['ftpPassword_2'] != undefined) || 
          ($_POST['ftpDate_2'] != undefined)
         )
 {
    $IPFtp_2=($_POST['ftpIP_2']);
    $UserFtp_2=($_POST['ftpUser_2']);
    $PasswordFtp_2=($_POST['ftpPassword_2']);
    $ftpName_2=$_POST['ftpName_2'];
    $ftpGiorni_2=$_POST['ftpDate_2'];
    
    $data_sec=time() + (60* 60* 24 * $ftpGiorni_1);
    $data_scedulazione=strftime('%Y-%m-%d %H:%M:%S',$data_sec);
    
    $query = "UPDATE ftp_table SET ftp_password='".$PasswordFtp_2."',ftp_user='".$UserFtp_2."',
        ftp_ip='".$IPFtp_2."',ftp_date=".$ftpGiorni_2.", data_scadenza='".$data_scedulazione."' WHERE ftp_name='".$ftpName_2."'";
    $Ftp_2_yes=($msql->execute($query)) ? 1: 0;
    
 }
 else if(($_POST['ora'] != undefined) || ($_POST['giorni'] != undefined))
 {
     $ora=($_POST['ora']);
     $query = "UPDATE data_schedulazione SET data = '$ora' WHERE id =1;";
     $stato=($msql->execute($query)) ? 1: 0;
     
     $ora=($_POST['giorni']);
     $query = "UPDATE data_schedulazione SET data = '$ora' WHERE id =2;";
     $stato=($msql->execute($query)) ? 1: 0;
 }

 if($admin_yes==null && $user_yes==null && $Ftp_1_yes==null && $Ftp_2_yes==null && $stato == null)
     $All_yes=1;
    
echo "$admin_yes,$user_yes,$Ftp_1_yes,$Ftp_2_yes,$stato,$All_yes";

?>

